package org.jeecg.modules.activiti.service;

import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.ManagementService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.activiti.command.Jump2TargetFlowNodeCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dangzhenghui
 */
@Service
@Transactional
public class SimpleTaskService {
	private static final Logger logger = LoggerFactory.getLogger(SimpleTaskService.class);

	@Autowired
	private TaskService taskService;
	@Autowired
	private ManagementService managementService;
	@Autowired
	private RepositoryService repositoryService;

	@Autowired
	private RuntimeService runtimeService;


	public Result complete(String processInstanceId, String comment, String expression) {
		Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
		if(task == null) {
			logger.error("process {} task is not exists", processInstanceId);
			return Result.error("流程任务不存在");
		}
		if(StringUtils.isNotEmpty(comment)) {
			taskService.addComment(task.getId(), processInstanceId, comment);
		}
		Map<String, Object> variables = new HashMap<>();
		variables.put("expression", expression);

		taskService.complete(task.getId(), variables);

		task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
		return Result.ok();
	}
	public String  startProcess(Map variables){
		Deployment deployment = repositoryService.createDeployment().name("银行审批测试demo")
				.addClasspathResource("processes/loanappr.bpmn").deploy();

		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
				.deploymentId(deployment.getId()).singleResult();
		ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
		Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
		taskService.setVariable(task.getId(),"form", JSONObject.toJSONString(variables));
		return processInstance.getId();
	}


	/**
	 * 跳转到指定流程节点
	 *
	 * @param curTaskId
	 * @param targetFlowNodeId
	 *            指定的流程节点ID 比如跳转<endEvent id="endevent1" name="End"></endEvent> ，则targetFlowNodeId为endevent1
	 */
	public void jump2TargetFlowNode(String curTaskId, String targetFlowNodeId) {
		managementService.executeCommand(new Jump2TargetFlowNodeCommand(curTaskId, targetFlowNodeId));
	}
}
